package com.umeox.mediaplayer

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.yc.music.inter.OnPlayerEventListener
import com.yc.music.model.AudioBean
import com.yc.music.tool.BaseAppHelper


class MusicPlayerControlView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : RelativeLayout(context, attrs, defStyleAttr), OnPlayerEventListener, View.OnClickListener {

    private var ivPrev: ImageView? = null
    private var ivPlay: ImageView? = null
    private var ivNext: ImageView? = null
    private var musicNameTv: TextView? = null
    private var mAudioBean: AudioBean? = null

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.view_music_control, this)
        musicNameTv =
            findViewById(R.id.tv_music_name)
        ivPrev = findViewById(R.id.iv_prev)
        ivPlay = findViewById(R.id.iv_play)
        ivNext = findViewById(R.id.iv_next)
        findViewById<View>(R.id.iv_control_remove).setOnClickListener(this)
        ivPrev?.setOnClickListener(this)
        ivPlay?.setOnClickListener(this)
        ivNext?.setOnClickListener(this)
        view.setOnClickListener(this)

        val musicService = BaseAppHelper.get().musicService
        mAudioBean = musicService?.playingMusic
        musicNameTv?.text = mAudioBean?.title
        ivPlay?.isSelected = musicService.isPlaying


    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        BaseAppHelper.get().musicService.addOnPlayEventListener(this)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        BaseAppHelper.get().musicService.removeOnPlayEventListener(this)
    }

    override fun onChange(audioBean: AudioBean) {
        mAudioBean = audioBean
        musicNameTv!!.text = audioBean.title
    }

    override fun onPlayerStart() {
        ivPlay!!.isSelected = true
    }

    override fun onPlayerPause() {
        ivPlay!!.isSelected = false
    }

    override fun onCompletion() {
    }

    override fun onUpdateProgress(progress: Int) {
    }

    override fun onBufferingUpdate(percent: Int) {
    }

    override fun onTimer(remain: Long) {
    }

    override fun onDuration(totalDuration: Int) {
    }

    override fun onClick(v: View) {

        when (v.id) {
            R.id.iv_play -> BaseAppHelper.get().musicService?.playPause()
            R.id.iv_next -> BaseAppHelper.get().musicService?.doNext()
            R.id.iv_prev -> BaseAppHelper.get().musicService?.doPrev()
            R.id.iv_control_remove -> (parent as FrameLayout).removeAllViews()
            else -> {
                val intent = Intent(context, MusicActivity::class.java)
                intent.putExtra(KEY_URI_MUSIC, mAudioBean)
                context.startActivity(intent)
            }
        }
    }


}